<?php
/**
 * Eren Framework
 * 
 * Validator Class
 * 
 * @category System
 * @author Manu Soros Leu
 * @copyright manusoros@gmail.com
 */

class Eren_Validator 
{
    /**
     * Varify an Object if is an instance of some class 
     * 
     * @param object-instance $class
     * @param string $baseClass
     * @return bool
     */
    public static function isBaseClass($class, $baseClass)
    {
        if(!($class instanceof $baseClass))
        {
            return FALSE;
        }
        return TRUE;
    }
    
    /**
     * Check if config is right 
     *
     * @param array $config
     * @param array $keyMust
     * @throws Eren_Exception
     * @return bool
     */
    public static function checkConfig($config, $keyMust)
    {
        foreach ($keyMust as $key){
            if(!isset($config[$key]) || empty($config[$key])){
                throw new Eren_Exception("Config is not right, key '{$key}' is not exit or be NULL ");
            }
        }
        return TRUE;
    }
    
    /**
     * Check the formate of email
     *
     * @param string $email
     * @throws Eren_Exception
     * @return bool
     */
    public static function checkEmail($email)
    {
        if(!is_string($email)){
            throw new Eren_Exception("The param of Eren_Validator::checkEmail must be string");
        }
        
        $pregEmail = "/^[a-z0-9_\-\.]+@[a-z0-9_\-\.]+[\.][a-z]{2,3}$/i";
        return preg_match($pregEmail,$email) == 1 ? TRUE : FALSE;  
    }
    
    /**
     * Check the formate of URL
     *
     * @param string $url
     * @throws Eren_Exception
     * @return bool
     */
    public static function checkUrl($url)
    {
        $urlinfo = parse_url($url);
        if(isset($urlinfo['scheme']) && isset($urlinfo['host'])){
            return TRUE;
        }
        return FALSE;
    }
}
